package org.teziyou.ticket.model;

import org.apache.log4j.Logger;

import java.util.List;

import org.teziyou.ticket.enumtype.CommentStarEnum;

import com.jfinal.ext.plugin.sqlinxml.SqlKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

public class ScenicComment extends Model<ScenicComment> {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ScenicComment.class);

	/**
	 * 
	 */
	private static final long serialVersionUID = -7087527162277948557L;
	
	public static final ScenicComment dao = new ScenicComment();
	
	public Page<ScenicComment> getScenicCommentsByPage(long scenicId, int pageNo, int pageSize) {
		String sql = SqlKit.sql("comment.getCommentsByScenicId");
		logger.info(sql);
		Page<ScenicComment> page = paginate(pageNo, pageSize, "select *", sql, scenicId);
		for(ScenicComment sc:page.getList()) {
			long memberId = sc.getLong("member_id");
			Member member = Member.dao.getMemberById(memberId);
			sc.put("member", member);
			
			int dictStarId = sc.getInt("dict_star_id");
			Dict starDict = Dict.dao.getDictById(dictStarId);
			starDict.put("enumtype", CommentStarEnum.getEnumByCode(dictStarId));
			sc.put("starDict", starDict);
			
			int dictServiceId = sc.getInt("dict_service_id");
			Dict serviceDict = Dict.dao.getDictById(dictServiceId);
			logger.info(serviceDict);
			sc.put("serviceDict", serviceDict);
			
			int dictTrafficId = sc.getInt("dict_traffic_id");
			Dict trafficDict = Dict.dao.getDictById(dictTrafficId);
			logger.info(trafficDict);
			sc.put("trafficDict", trafficDict);
		}
		return page;
	}
	
	public List<Record> summaryComments(long scenicId) {
		String sql = SqlKit.sql("comment.summaryComments");
		List<Record> records = Db.use("commodity").find(sql, scenicId);
		for(Record r:records) {
			int dictStarId = r.getInt("dict_star_id");
			r.set("enumtype", CommentStarEnum.getEnumByCode(dictStarId));
		}
		return records;
	}
	
}
